package org.example.controller;

import org.example.Entity.CommodityMapRecognition;
import org.example.Entity.Result;
import org.example.utils.ApiClient;
import org.example.utils.GsonUtil;
import org.springframework.web.bind.annotation.*;

/**
 * AI识图API
 */
@RestController
@RequestMapping("/api/ai")
public class AiIdentifyController {

    @PostMapping("/picture")
    public Result<Object> uploadImage(@RequestBody CommodityMapRecognition cmr) {
        String picture_path = cmr.getPicture_path();
        System.out.println("图片地址："+picture_path);
        String result = ApiClient.commodityMapRecognition(picture_path);
        if (result != null){
            CommodityMapRecognition cmr2 = (CommodityMapRecognition)GsonUtil.jsonToObject(result, CommodityMapRecognition.class);
            if (cmr2.getPredicted_label() != null){
                return Result.success(cmr2);
            }else{
                return Result.error(result);
            }
        }
        return Result.error("视图失败，请重试！");
    }
}
